package com.qujia.station.controller;

import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.station.domain.Station;
import com.qujia.station.domain.Tank;
import com.qujia.station.service.IStationService;
import com.qujia.station.service.ITankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 油站信息Controller
 *
 * @author qujia
 * @date 2020-12-08
 */
@RestController
@RequestMapping("/tank/info")
public class TankController extends BaseController
{
    @Autowired
    private ITankService tankService;
    @Autowired
    private IStationService stationService;

    /**
     * 查询油站信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Tank tank)
    {
        startPage();
        List<Tank> tankList=tankService.selectTankList(tank);
        return getDataTable(tankList);
    }

    /**
     * 新增油品
     */
    @Log(title = "增加油管", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tank tank)
    {
        //更新油站修改时间
        Station updateStation = new Station();
        updateStation.setStationId(tank.getStationId());
        stationService.updateStation(updateStation);

        return toAjax(tankService.insertTank(tank));
    }

    /**
     * 获取油站信息详细信息
     */
    @GetMapping(value = "/{tankId}")
    public AjaxResult getInfo(@PathVariable("tankId") Long tankId)
    {
        Tank tank=tankService.selectTankById(tankId);

        return AjaxResult.success(tank);
    }

    /**
     * 修改油站信息
     */
    @Log(title = "修改油罐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tank tank)
    {
        //更新油站修改时间
        Station updateStation = new Station();
        updateStation.setStationId(tank.getStationId());
        stationService.updateStation(updateStation);

        return toAjax(tankService.updateTank(tank));
    }

    /**
     * 删除油站支付管理图片
     */
    @Log(title = "删除油罐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{thankId}")
    public AjaxResult remove(@PathVariable Long thankId)
    {
        //更新油站修改时间
        Tank tank = tankService.selectTankById(thankId);
        Station updateStation = new Station();
        updateStation.setStationId(tank.getStationId());
        stationService.updateStation(updateStation);

        return toAjax(tankService.deleteTankById(thankId));
    }

}
